<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#-- 包含其他文件的内容 -->
    <#include "../common/header.ftl"/>
    <script src="/js/plugins/jqueryForm/jquery.form.js"></script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
            <#-- 往数据模型中填充一个属性 -->
            <#assign menu="strategy"/>
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">大攻略管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategy/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}">
                <input type="submit" value="查询">
                <button type="button" class="btn-info inputBtn">添加</button>
            </form>

            <table class="table table-striped table-hover" >
                <tr>
                    <th>编号</th>
                    <th>封面</th>
                    <th>攻略标题</th>
                    <th>副标题</th>
                    <th>所属地区</th>
                    <th>状态</th>
                    <th></th>
                </tr>
                <#-- 循环迭代指令 -->
                <#list pageInfo.list as entity>
                    <tr>
                       <td>${entity_index + 1}</td>
                       <td><img src="${entity.coverUrl}" width="60px"></td>
                       <td>${entity.title}</td>
                       <td>${entity.subTitle}</td>
                       <td>${(entity.place.name)!} </td>
                       <td>${entity.stateName}</td>
                        <td><a role="button" class="inputBtn" data-json='${entity.json}'>修改</a></td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl"/>
        </div>
    </div>
</div>
<div class="modal fade" id="strategyModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">编辑大攻略</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategy/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden"  name="id">
                    <div class="form-group" >
                        <label class="col-sm-4 control-label">标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="title">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" >副标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="subTitle" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" >封面：</label>
                        <div class="col-sm-6">
                            <img  id="coverImg"  width="100%" >
                            <input type="hidden" name="coverUrl">
                            <button type="button" id="fileButten">选择文件</button>

                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" >所属地区：</label>
                        <div class="col-sm-6">
                            <select name="place.id" class="form-control">

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" >状态：</label>
                        <div class="col-sm-6">
                            <select name="state" class="form-control">
                                <option value="0">普通</option>
                                <option value="1">热门</option>
                                <option value="-1">禁用</option>
                            </select>
                        </div>
                    </div>

                </form>
                <form id="fileForm" action="/images/xx.do" method="post" enctype="multipart/form-data"/>
                 <input id="fileBtn" type="file" class="form-control" name="file" style="display: none" accept="image/*" >
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">
                    <span class="glyphicon glyphicon-off"></span> 关闭
                </button>
                <button type="button" class="btn btn-primary submitBtn">
                    <span class="glyphicon glyphicon-saved"></span> 保存
                </button>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        //模态框所有地区列表
        $.get("/region/listAll.do",function (data) {
            var option;
            $.each(data,function (index,value) {
                option +="<option value='"+value.id+"'>"+value.name+"</option>"
                $("#editForm select[name='place.id']").html(option)
            })

        })
        //模态框中的选择文件按钮
        $("#fileButten").click(function () {
            $("#fileBtn").click();
        })
        //提交图片
        $("#fileBtn").change(function () {
            if ($(this).val()) {
                $("#fileForm").ajaxSubmit(function (data) {
                    if (data.url) {
                        $("#coverImg").prop("src",data.url)
                    }
                })
            }
        })




        //添加编辑按钮
        $(".inputBtn").click(function () {
            var json =$(this).data("json");
            $("#coverImg").prop("src","");
            $("#strategyModal input,#strategyModal select").val("");
            if (json) {
                $("#strategyModal input[name='id']").val(json.id);
                $("#strategyModal input[name='title']").val(json.title);
                $("#strategyModal input[name='subTitle']").val(json.subTitle);
                $("#strategyModal select[name='place.id']").val(json.place.id);
                $("#strategyModal select[name='state']").val(json.state);
                $("#coverImg").prop("src",json.coverUrl)
            }
            $("#strategyModal").modal("show")
        })

        $(".submitBtn").click(function () {
            $("#strategyModal input[name='coverUrl']").val($("#coverImg").attr("src"));
           $("#editForm").ajaxSubmit(function (data) {
               if (data.success) {
                   reload();
               }else {
                   $.messager.alert(data.msg);
               }
           })
        })
    })
</script>
</body>
</html>